﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dazaza.UI.Mvc.Models;
using Dazaza.Repositories;

namespace Dazaza.UI.Mvc.Controllers
{
    public class BaseController : Controller
    {
        protected MasterViewModel masterViewModel = new MasterViewModel();

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            masterViewModel.HeaderInfo = new HeaderInfo() { PageTitle = "diglog" };

            //分页信息
            string pageindex = Request.QueryString["pageindex"] ?? "1";
            if (pageindex != string.Empty)
            {
                int _pageindex = 1;
                if (int.TryParse(pageindex, out _pageindex))
                {
                    masterViewModel.PagerInfo.PageIndex = _pageindex;
                }
                else
                {
                    _pageindex = 1;
                }
            }


            //获取当前分类
            string categoryName = Request.QueryString["category"] ?? string.Empty;
            if (categoryName == string.Empty)
            {
                //一级分类
                List<Dazaza.Domain.Model.CategoryClassModel> listCategoryClass = CategoryRepository.GetCategoryClassList();
                if (listCategoryClass != null)
                {
                    foreach (var item in listCategoryClass)
                    {
                        masterViewModel.CategoryList.Add(
                            new Models.CategoryCommon()
                            {
                                ID = item.ClassID,
                                Name = item.Name,
                                Name_En = item.Name_En,
                                Sort = item.sort
                            });
                    }
                }

            }
            else
            {
                //分类的子分类
                List<Dazaza.Domain.Model.CategoryModel> listCategory = CategoryRepository.GetCategoryListByClassName(categoryName);
                if (listCategory != null)
                {
                    foreach (var item in listCategory)
                    {
                        masterViewModel.CategoryList.Add(
                            new Models.CategoryCommon()
                            {
                                ID = item.CategoryID,
                                Name = item.Name,
                                Name_En = item.Name_En,
                                Sort = item.Sort
                            });
                    }
                }

            }

            base.OnActionExecuting(filterContext);
        }
    }
}
